package com.jshoperxms.controller.api.langdu;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.PrimaryKey;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.entity.MemberT;
import com.jshoperxms.entity.YdGrowtreeRdT;
import com.jshoperxms.entity.YdStatusInfoT;
import com.jshoperxms.entity.YdTreeInfoT;
import com.jshoperxms.service.MemberTService;
import com.jshoperxms.service.YdGrowtreeRdTService;
import com.jshoperxms.service.YdStatusInfoTSerivce;
import com.jshoperxms.service.YdTreeInfoTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by sdywcd on 2018/10/8.
 * Des:
 */
@RestController
@RequestMapping("/api/trees")
public class YdTreeController extends BaseTController{



    @Resource
    private YdStatusInfoTSerivce ydStatusInfoTSerivce;

    @Resource
    private YdTreeInfoTService ydTreeInfoTService;

    @Resource
    private YdGrowtreeRdTService ydGrowtreeRdTService;

    @Resource
    private MemberTService memberTService;

    /**
     *  成长页面初始化
     * @param rq
     * @return
     */
    @RequestMapping(value="/initSmaillTreeGrow",method= RequestMethod.POST)
    public ResponseEntity initSmaillTreeGrow(@RequestBody YdTreeInfoT rq) {
        Map<String, Object> responsedata = new HashMap<>();
        if (StringUtils.isNotBlank(rq.getWxopenid())) {
            Criterion criterion = Restrictions.eq("wxopenid", rq.getWxopenid());
            YdStatusInfoT bean = this.ydStatusInfoTSerivce.findOneByCriteria(YdStatusInfoT.class, criterion);
            if (bean != null) {
                responsedata.put("totalleafs", bean.getTotalleafs());
            }
            Criterion criterion1 = Restrictions.and(Restrictions.eq("iscurrentgrow", 1)).add(Restrictions.eq("wxopenid", rq.getWxopenid()));
            YdTreeInfoT bean1 = this.ydTreeInfoTService.findOneByCriteria(YdTreeInfoT.class, criterion1);
            if (bean1 != null) {
                responsedata.put("tree", bean1);
            }
            responsedata.put(TagWords.SUCCESS, true);
        } else {
            responsedata.put(TagWords.SUCCESS, false);
        }
        return Response.success(responsedata);

    }


    /**
     * 点击给树成长
     * @param rq
     * @return
     */
    @RequestMapping(value="/saveGrowTreeRd",method= RequestMethod.POST)
    public ResponseEntity saveGrowTreeRd(@RequestBody YdGrowtreeRdT rq) {
        Map<String, Object> responsedata = new HashMap<>();
        if (StringUtils.isNotBlank(rq.getWxopenid())) {
            Criterion criterion = Restrictions.eq("wxopenid", rq.getWxopenid());
            YdStatusInfoT bean1 = this.ydStatusInfoTSerivce.findOneByCriteria(YdStatusInfoT.class, criterion);
            if (bean1 != null) {
                if(bean1.getTotalleafs()>=10){
                    YdTreeInfoT beantree=this.ydTreeInfoTService.findByPK(YdTreeInfoT.class,rq.getTreeid());
                    if(beantree!=null) {
                        //增加成长记录
                        YdGrowtreeRdT bean = new YdGrowtreeRdT();
                        bean.setId(PrimaryKey.get().toString());
                        bean.setCurrentleafs(beantree.getCurrentleafs() + beantree.getPergrow());
                        bean.setStatus(BaseEnums.DataUsingState.USING.getState());
                        bean.setCreatetime(DateTools.getSystemTimeLong());
                        bean.setCreatorid("0");
                        bean.setUpdatetime(DateTools.getSystemTimeLong());
                        bean.setVersiont(1);
                        bean.setTreeid(beantree.getId());
                        bean.setPergrow(beantree.getPergrow());
                        bean.setCurrentgrowstatus(beantree.getCurrentgrowstatus());
                        bean.setWxopenid(beantree.getWxopenid());
                        bean.setMemberid(beantree.getMemberid());
                        bean.setLastleafs(beantree.getCurrentleafs());
                        bean.setIssurprise(0);
                        this.ydGrowtreeRdTService.save(bean);
                        //减去树叶
                        bean1.setTotalleafs(bean1.getTotalleafs()-beantree.getPergrow());
                        this.ydStatusInfoTSerivce.update(bean1);
                        //更新树信息
                        beantree.setCurrentleafs(bean.getCurrentleafs());
                        if(bean.getCurrentleafs()==1000){
                            beantree.setIscurrentgrow(0);
                            beantree.setCurrentgrowstatus(5);
                            responsedata.put(TagWords.MSG,"growover");
                        }
                        this.ydTreeInfoTService.update(beantree);
                        responsedata.put(TagWords.SUCCESS,true);

                    }
                }else{
                    responsedata.put(TagWords.SUCCESS,false);
                    responsedata.put("enough",false);
                    responsedata.put(TagWords.MSG,"树叶不足");
                }
            }
        }else{
            responsedata.put(TagWords.SUCCESS, false);
        }
        return Response.success(responsedata);
    }





}
